package com.bah.tract.mapping;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bah.tract.service.BooleanProperty;
import com.bah.tract.service.DateProperty;
import com.bah.tract.service.DoubleProperty;
import com.bah.tract.service.LongProperty;
import com.bah.tract.service.Property;
import com.bah.tract.service.StringProperty;

public final class PropertyHelper {
	private PropertyHelper() {

	}

	public static Property getProperty(final String name, final Object value) {
		Property prop;
		if (value instanceof String) {
			prop = new StringProperty(name, (String) value);
		} else if (value instanceof Long) {
			prop = new LongProperty(name, (Long) value);
		} else if (value instanceof Boolean) {
			prop = new BooleanProperty(name, (Boolean) value);
		} else if (value instanceof Double) {
			prop = new DoubleProperty(name, (Double) value);
		} else if (value instanceof Date) {
			prop = new DateProperty(name, (Date) value);
		} else {
			throw new IllegalArgumentException("Unsupported type: "
					+ value.getClass().getName());
		}
		return prop;
	}

	public static Object getPropertyValue(final Property prop) {
		Object value = null;
		switch (prop.getType()) {
		case DATE_TIME:
			value = prop.getDateTimeValue().toGregorianCalendar().getTime();
			break;
		case LONG:
			value = prop.getLongValue();
			break;
		case DOUBLE:
			value = prop.getDoubleValue();
			break;
		case STRING:
			value = prop.getStringValue();
			break;
		case BOOLEAN:
			value = prop.isBooleanValue();
			break;
		}
		return value;
	}

	public static void processProperties(final Map<String, Object> properties,
			final List<Property> props) {
		for (final Property prop : props) {
			properties.put(prop.getName(), getPropertyValue(prop));
		}
	}

}
